package comgame.components;

import com.almasb.fxgl.entity.component.Component;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.util.Duration;
import java.util.Random;

public class KingFishMovementComponent extends Component {
    private static final double SPEED = 30; // 鱼王移动速度稍快
    private static final double SCREEN_WIDTH = 800;
    private static final double SCREEN_HEIGHT = 600;
    private static final double RANGE_X = SCREEN_WIDTH * 5.0;
    private static final double RANGE_Y = SCREEN_HEIGHT * 5.0;
    private Point2D target;
    private Random random = new Random();
    private double timer = 0;
    private double nextTargetInterval = 3.0; // 初始间隔
    private double currentAngle = 0; // 当前朝向角度（弧度）
    private int presetIndex = 0; // 顺序目标点索引

    // 预设目标点（屏幕外1.75倍范围内）
    private static final Point2D[] PRESET_TARGETS = new Point2D[] {
        new Point2D(-1.5 * SCREEN_WIDTH, -1.5 * SCREEN_HEIGHT), // 屏幕外左上
        new Point2D(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2),   // 屏幕中心
        new Point2D(1.75 * SCREEN_WIDTH, 1.75 * SCREEN_HEIGHT),   // 屏幕外右下
        new Point2D(-1.5 * SCREEN_WIDTH, SCREEN_HEIGHT * 0.5),// 屏幕外左中
        new Point2D(1.75 * SCREEN_WIDTH, SCREEN_HEIGHT * 0.5)  // 屏幕外右中
    };

    @Override
    public void onAdded() {
        setNewTarget();
        nextTargetInterval = getRandomInterval();
        // 初始朝向指向目标
        Point2D pos = entity.getPosition();
        Point2D dir = target.subtract(pos);
        currentAngle = Math.atan2(dir.getY(), dir.getX());
    }

    @Override
    public void onUpdate(double tpf) {
        if (entity == null) return;
        timer += tpf;
        // 只有到达目标点或到达间隔才换目标
        if (entity.getPosition().distance(target) < 8 || timer > nextTargetInterval) {
            setNewTarget();
            timer = 0;
            nextTargetInterval = getRandomInterval();
        }
        Point2D pos = entity.getPosition();
        Point2D dir = target.subtract(pos);
        if (dir.magnitude() > 1e-3) {
            double targetAngle = Math.atan2(dir.getY(), dir.getX());
            // 平滑插值角度
            currentAngle = lerpAngle(currentAngle, targetAngle, 0.035); // alpha可调
            double dx = Math.cos(currentAngle) * SPEED * tpf;
            double dy = Math.sin(currentAngle) * SPEED * tpf;
            entity.translate(dx, dy);
            // 更新实体旋转以匹配移动方向
            entity.setRotation(Math.toDegrees(currentAngle));
        }
    }

    private void setNewTarget() {
        // 按顺序循环目标点
        Point2D pos = entity.getPosition();
        Point2D preset = PRESET_TARGETS[presetIndex];
        presetIndex = (presetIndex + 1) % PRESET_TARGETS.length;
        target = preset;
    }

    private double getRandomInterval() {
        // 12~20秒之间，给鱼王更多时间游到目标点
        return 12.0 + random.nextDouble() * 8.0;
    }

    // 角度插值，处理弧度环绕
    private double lerpAngle(double a, double b, double t) {
        double diff = b - a;
        while (diff > Math.PI) diff -= 2 * Math.PI;
        while (diff < -Math.PI) diff += 2 * Math.PI;
        return a + diff * t;
    }
} 